package com.pdg.android.rockscissorspaper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class PlayActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);
        initializeDisplay();
    }

	private void initializeDisplay() {
		TextView header = (TextView)findViewById(R.id.header);
		Button button = (Button)findViewById(R.id.finish_button);
		if(Settings.getRoundsFixed(this)){
			header.setText(String.format(getString(R.string.limited_rounds),Settings.getRoundCount(this)));
			button.setText(getString(R.string.forfeit_match));
		}else{
			header.setText(getString(R.string.unlimited_rounds));
			button.setText(getString(R.string.end_match));
		}
		TextView text = (TextView)findViewById(R.id.text);
		text.setText(getString(R.string.no_last_round));
		Settings.setRoundNumber(this,1);
		Settings.setHumanScore(this,0);
		Settings.setComputerScore(this,0);
		updateScores();
	}
	
	private void updateScores() {
		TextView text = (TextView)findViewById(R.id.human_score);
		text.setText(String.format(getString(R.string.you_have_won),Settings.getHumanScore(this)));
		text = (TextView)findViewById(R.id.computer_score);
		text.setText(String.format(getString(R.string.i_have_won),Settings.getComputerScore(this)));
	}

	public void rockButtonPressed(View theView){
		makeMove(MoveTypes.ROCK);
	}
	private void makeMove(MoveTypes theMove) {
		MoveTypes opponentMove = Utilities.randomMove();
		MoveResults result = theMove.results(opponentMove);
		switch(result){
		case WIN:
			Settings.setRoundNumber(this, Settings.getRoundNumber(this)+1);
			Settings.setHumanScore(this,Settings.getHumanScore(this)+1);
			switch(theMove){
			case ROCK:
				Settings.setNumberOfRockWins(this,1+Settings.getNumberOfRockWins(this));
				break;
			case SCISSORS:
				Settings.setNumberOfScissorsWins(this,1+Settings.getNumberOfScissorsWins(this));
				break;
			case PAPER:
				Settings.setNumberOfPaperWins(this,1+Settings.getNumberOfPaperWins(this));
				break;
			}
			break;
		case TIE:
			switch(theMove){
			case ROCK:
				Settings.setNumberOfRockTies(this,1+Settings.getNumberOfRockTies(this));
				break;
			case SCISSORS:
				Settings.setNumberOfScissorsTies(this,1+Settings.getNumberOfScissorsTies(this));
				break;
			case PAPER:
				Settings.setNumberOfPaperTies(this,1+Settings.getNumberOfPaperTies(this));
				break;
			}
			break;
		case LOSE:
			Settings.setRoundNumber(this, Settings.getRoundNumber(this)+1);
			Settings.setComputerScore(this,Settings.getComputerScore(this)+1);
			switch(theMove){
			case ROCK:
				Settings.setNumberOfRockLosses(this,1+Settings.getNumberOfRockLosses(this));
				break;
			case SCISSORS:
				Settings.setNumberOfScissorsLosses(this,1+Settings.getNumberOfScissorsLosses(this));
				break;
			case PAPER:
				Settings.setNumberOfPaperLosses(this,1+Settings.getNumberOfPaperLosses(this));
				break;
			}
			break;
		}
		if(Settings.getRoundsFixed(this)){
			if(Settings.getHumanScore(this)>=Settings.getRoundCount(this)){
				Settings.setNumberOfMatchWins(this,Settings.getNumberOfMatchWins(this)+1);
		        Intent myIntent = new Intent(this,WinActivity.class);
		        startActivityForResult(myIntent, 0);
			}else if(Settings.getComputerScore(this)>=Settings.getRoundCount(this)){
				Settings.setNumberOfMatchLosses(this,Settings.getNumberOfMatchLosses(this)+1);
		        Intent myIntent = new Intent(this,LossActivity.class);
		        startActivityForResult(myIntent, 0);
			}
		}
		TextView tv = (TextView)findViewById(R.id.text);
		tv.setText(String.format(getString(R.string.last_round_summary),getString(opponentMove.getResourceId()),getString(theMove.getResourceId()),getString(result.getResourceId())));
		updateScores();
	}

	public void scissorsButtonPressed(View theView){
		makeMove(MoveTypes.SCISSORS);
	}
	public void paperButtonPressed(View theView){
		makeMove(MoveTypes.PAPER);
	}
	public void backButtonPressed(View theView){
		if(Settings.getRoundsFixed(this)){
			Settings.setNumberOfMatchLosses(this,Settings.getNumberOfMatchLosses(this)+1);
		}
        Intent myIntent = new Intent(theView.getContext(), MainMenuActivity.class);
        startActivityForResult(myIntent, 0);
	}

}
